package logic

import (
	"MoSkeleton/config/consts"
	"MoSkeleton/framework/utils"
	"MoSkeleton/services/tool/logic/internal"
	"bytes"
	"path/filepath"
)

type DatasourcePogo struct {
	DatasourceName string `json:"datasourceName" form:"datasourceName" binding:"required"`
	DatasourceType string `json:"datasourceType" form:"datasourceType"`
	Url            string `json:"url" form:"url"`
	ServerAddr     string `json:"serverAddr" form:"serverAddr"`
	Port           int32  `json:"port" form:"port"`
	DbName         string `json:"dbName" form:"dbName"`
	DbParam        string `json:"dbParam" form:"dbParam"`
	DbUsername     string `json:"dbUsername" form:"dbUsername"`
	DbPasswd       string `json:"dbPasswd" form:"dbPasswd"`
	ListOrder      int32  `json:"listOrder" form:"listOrder"`
	Status         string `json:"status" form:"status"`
	Remark         string `json:"remark" form:"remark"`
	CreatedBy      string `json:"createdBy" form:"createdBy"`
	UpdatedBy      string `json:"updatedBy" form:"updatedBy"`
}

type TplFileBuffer struct {
	//模板文件类型：1:model 2:service 3:controller
	TplFileType       int
	TplFileName       string
	OutFileName       string
	PackagePath       string
	ModuleName        string
	IgnoreTablePrefix bool
	StructName        string
	TablePrefix       string
	Buffer            *bytes.Buffer
}

func (t *TplFileBuffer) GenOutFileNameFromTplFileName(tableName string) {
	t.OutFileName = t.getOutFileName(tableName) + t.getTplFileSuffix()
}

func (t *TplFileBuffer) GetOutFilePathName() string {
	switch t.TplFileType {
	case consts.TplType_Sql, consts.TplType_Api_Struct:
		return filepath.Join(consts.TplType_Path_Map[t.TplFileType], t.PackagePath, t.OutFileName)
	case consts.TplType_Struct_Index:
		return filepath.Join(consts.TplType_Path_Map[t.TplFileType], t.PackagePath, FirstLetterLower(t.StructName), t.OutFileName)
	default:
		return filepath.Join(t.ModuleName, consts.TplType_Path_Map[t.TplFileType], t.PackagePath, t.OutFileName)
	}
}

func (t *TplFileBuffer) GetPackagePathForAggregate() {
	switch t.TplFileType {
	case consts.TplType_Router_Makeup:
		t.PackagePath = "internal"
	}
	t.PackagePath = ""
}

func (t *TplFileBuffer) getTplFileSuffix() string {
	switch t.TplFileType {
	case consts.TplType_Model, consts.TplType_Service, consts.TplType_Controller,
		consts.TplType_Req, consts.TplType_Resp, consts.TplType_Router_Makeup,
		consts.TplType_Ctrl_Import_Vars, consts.TplType_Ctrl_Export_Vars,
		consts.TplType_Serv_Import_Vars, consts.TplType_Serv_Export_Vars:
		return ".go"
	case consts.TplType_Sql:
		return ".sql"
	case consts.TplType_Struct_Index:
		return ".vue"
	case consts.TplType_Api_Struct:
		return ".js"
	default:
		return ".unknow"
	}
}
func (t *TplFileBuffer) getOutFileName(tableName string) string {
	switch t.TplFileType {
	case consts.TplType_Service:
		return internal.FileNameFromTable(tableName, t.IgnoreTablePrefix, t.TablePrefix) + "_serv"
	case consts.TplType_Controller:
		return internal.FileNameFromTable(tableName, t.IgnoreTablePrefix, t.TablePrefix) + "_ctrl"
	case consts.TplType_Req:
		return internal.FileNameFromTable(tableName, t.IgnoreTablePrefix, t.TablePrefix) + "_req"
	case consts.TplType_Resp:
		return internal.FileNameFromTable(tableName, t.IgnoreTablePrefix, t.TablePrefix) + "_resp"
	case consts.TplType_Router_Makeup:
		//去掉.go.tpl 后缀
		return utils.GetFileNameWithoutExtension(utils.GetFileNameWithoutExtension(t.TplFileName))
	case consts.TplType_Ctrl_Import_Vars, consts.TplType_Serv_Import_Vars:
		return "import_vars"
	case consts.TplType_Ctrl_Export_Vars, consts.TplType_Serv_Export_Vars:
		return "export_vars"
	case consts.TplType_Api_Struct:
		return FirstLetterLower(t.StructName)
	case consts.TplType_Struct_Index:
		return "index"
	}
	return internal.FileNameFromTable(tableName, t.IgnoreTablePrefix, t.TablePrefix)
}
